package com.chenyongjing.design.pattern.visitor.orgin;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;

/**
 * @Author chen
 * @Date 2022/12/11 22:18
 */
public class FileVisitorTest {
    public static void main(String[] args) {
        try{
            Files.walkFileTree(Paths.get("F:","/J2EEStudy/design-pattern/src/main/java/com/chenyongjing/design/pattern/visitor/orgin")
                    ,new SimpleFileVisitor<Path>(){
                        public FileVisitResult preVisitDirectory(Path dir,BasicFileAttributes attrs) throws IOException{
                            System.out.println("正在访问"+dir+"目录");
                            return FileVisitResult.CONTINUE;
                        }

                        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException{
                            System.out.println("正在访问"+file+"文件");
                            if(file.endsWith("FileVisitorTest.java")){
                                System.out.println("------------FileVisitorTest.java，文件内容----------");
                                List<String> list = Files.readAllLines(file);
                                System.out.println(list);
                                return FileVisitResult.TERMINATE;
                            }
                            return FileVisitResult.CONTINUE;
                        }

                        public FileVisitResult visitResultFaild(Path file,IOException exc) throws IOException{
                            //写一些具体的业务逻辑
                            return super.visitFileFailed(file,exc);
                        }

                        public FileVisitResult postVisitDirectory(Path dir,IOException exc) throws IOException{
                            return super.postVisitDirectory(dir,exc);
                        }
                    });


        }catch (Exception e){
            e.printStackTrace();
        }
    }




}
